package com.awesome.uidemo.dragview;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2022/1/5
 * Description:我的适配器
 */
public class MyAdapter extends FragmentStateAdapter {
    public List<Class> list_fragment;
    public List<Object> list_data;
    public List<Fragment> listFragment;

    public MyAdapter(FragmentActivity activity, List<Class> list_fragment, List<Object> list_data) {
        super(activity);
        this.list_fragment = list_fragment;
        this.list_data = list_data;
        listFragment = new ArrayList<>();
    }

    @NonNull
    @NotNull
    @Override
    public Fragment createFragment(int position) {
        if (listFragment != null && listFragment.size() > position && listFragment.get(position) != null) {
            return listFragment.get(position);
        }
        try {
            if (listFragment.size() == position) {
                Fragment fragment = (Fragment) (list_fragment.get(position)).newInstance();
                if (fragment instanceof DragFragment) {
                    ((DragFragment) fragment).setBundlePosition(position);
                    ((DragFragment) fragment).data = list_data.get(position);
                }
                listFragment.add(fragment);
                return listFragment.get(position);

            } else if (listFragment.size() > position) {

                if (listFragment.get(position) == null) {
                    listFragment.set(position, (Fragment) (list_fragment.get(position)).newInstance());
                    if (listFragment.get(position) instanceof DragFragment) {
                        ((DragFragment) listFragment.get(position)).setBundlePosition(position);
                        ((DragFragment) listFragment.get(position)).data = list_data.get(position);
                    }
                }
                return listFragment.get(position);
            } else {
                for (int i = listFragment.size(); i < position; i++) {
                    listFragment.add(null);
                }
                Fragment fragment = (Fragment) (list_fragment.get(position)).newInstance();
                if (fragment instanceof DragFragment) {
                    ((DragFragment) fragment).setBundlePosition(position);
                    ((DragFragment) fragment).data = list_data.get(position);

                }
                listFragment.add(fragment);
                return listFragment.get(position);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return list_fragment.size();
    }


}

 